﻿using System;
using System.Data.Entity;
using System.ServiceModel;
using Metro.Kashikoi.Service.Common.Management;
using Metro.Kashikoi.Service.Location.Entity;
using Metro.Kashikoi.Service.Location.Service;

namespace Metro.Kashikoi.Service.Location
{
    public class ServiceManager : ServiceManagerBase
    {
        private ServiceHost host;

        #region ServiceManagerBase Members

        public override DiagnoseResult Diagnose()
        {
            throw new NotImplementedException();
        }

        protected override void DoInitialize()
        {
            Database.SetInitializer<Context>(new DropCreateDatabaseIfModelChanges<Context>());
            this.host = new ServiceHost(typeof(LocationService));
        }

        protected override void DoStart()
        {
            this.host.Open();
        }

        protected override void DoStop()
        {
            if (this.host != null && host.State == CommunicationState.Opened)
            {
                this.host.Close();
            }
        } 

        #endregion

        #region Disposable Members

        public override void Disposing()
        {
            if (this.host != null && host.State == CommunicationState.Opened)
            {
                this.host.Close();
                this.host = null;
            }
        }

        #endregion
    }
}
